const api = require('../../api/api.js');
const util = require('../../utils/util.js');
const app = getApp();
Page({
  data: {
    // items: [{
    //   image: 'icon_site.png',
    //   color: 'rgba(13,47,138,0.95)',
    //   shadow: 'rgba(13,47,138,0.95)',
    //   title: '基地',
    //   link: 'site-a'
    // }, {
    //   image: 'icon_land.png',
    //   color: 'rgba(13,47,138,0.95)',
    //   shadow: 'rgba(13,47,138,0.95)',
    //   title: '地块',
    //   link: 'place-a'
    // }, {
    //   image: 'icon_crop.png',
    //   color: 'rgba(13,47,138,0.95)',
    //   shadow: 'rgba(13,47,138,0.95)',
    //   title: '作物',
    //   link: 'crop'
    // }, {
    //   image: 'icon_livestock.png',
    //   color: 'rgba(13,47,138,0.95)',
    //   shadow: 'rgba(13,47,138,0.95)',
    //   title: '牲畜',
    //   link: 'livestock'
    // }, {
    //   image: 'icon_fungus.png',
    //   color: 'rgba(13,47,138,0.95)',
    //   shadow: 'rgba(13,47,138,0.95)',
    //   title: '菌类',
    //   link: 'fungus'
    // }, {
    //   image: 'icon_good.png',
    //   color: 'rgba(13,47,138,0.95)',
    //   shadow: 'rgba(13,47,138,0.95)',
    //   title: '商品',
    //   link: 'good'
    // }, {
    //   image: 'icon_warehouse.png',
    //   color: 'rgba(13,47,138,0.95)',
    //   shadow: 'rgba(13,47,138,0.95)',
    //   title: '仓库',
    //   link: 'warehouse'
    // }, {
    //   image: 'icon_certification.png',
    //   color: 'rgba(13,47,138,0.95)',
    //   shadow: 'rgba(13,47,138,0.95)',
    //   title: '证书',
    //   link: 'certification'
    // }, {
    //   image: 'icon_claim.png',
    //   color: 'rgba(13,47,138,0.95)',
    //   shadow: 'rgba(13,47,138,0.95)',
    //   title: '认领',
    //   link: 'claim'
    // }, {
    //   image: 'icon_scan.png',
    //   color: 'rgba(13,47,138,0.95)',
    //   shadow: 'rgba(13,47,138,0.95)',
    //   title: '扫码',
    //   link: 'scan'
    // }, {
    //   image: 'icon_setting.png',
    //   color: 'rgba(13,47,138,0.95)',
    //   shadow: 'rgba(13,47,138,0.95)',
    //   title: '设置',
    //   link: 'setting'
    // }],
    login: false,
    isBind: true,
    current: 0,
    imgUrl: app.globalData.imgUrl + 'userImage/',
    user_copy: {
      image: '/imgs/default_accountimg.png',
      userName: '您尚未登录',
      email: '点击登录'
    }
  },
  onLoad() {
    let that = this;
    wx.getStorage({
      key: 'type',
      success(res) {
        that.setData({
          type: res.data
        });
        if (res.data === 'normal') {
          wx.getStorage({
            key: 'userId',
            success: function(res) {
              api.queryAccountData({
                data: {
                  account: res.data
                },
                success(res) {
                  console.log(res.data);
                  let user = res.data.accountInfo;
                  user.image = that.data.imgUrl + user.image
                  that.setData({
                    user,
                    login: true
                  });
                }
              });
            }
          })
        } else if (res.data === 'wx') {
          wx.getStorage({
            key: 'wxName',
            success(res) {
              let wxName = res.data;
              wx.getStorage({
                key: 'wxImg',
                success(res) {
                  let wxImg = res.data;
                  let user = {
                    image: wxImg,
                    userName: wxName,
                    email: '微信登录用户'
                  }
                  that.setData({
                    user,
                    login: true
                  });
                }
              })
            }
          });
          util.getLocalStorage(['isBind', 'openid'], that);
        }
        that.bindRole();
      },
      fail() {
        let user = that.data.user_copy;
        that.setData({
          login: false,
          user
        });
      }
    });
  },
  init() {
    let that = this;
    let role = that.data.role;
    let top = role === 0 || role === 4; //超管和企业家的权限
    let mid = role === 0 || role === 4 || role === 1; //基地管理的权限
    let normal = role === 0 || role === 4 || role === 1 || role === 2; //普通用户的权限
    let items = [{
        image: 'icon_company.png',
        color: 'rgba(13,47,138,0.95)',
        shadow: 'rgba(13,47,138,0.95)',
        title: '企业',
        link: 'company',
        level: top
      }, {
        image: 'icon_site.png',
        color: 'rgba(13,47,138,0.95)',
        shadow: 'rgba(13,47,138,0.95)',
        title: '基地',
        link: 'site-a',
        level: mid
      }, {
        image: 'icon_land.png',
        color: 'rgba(13,47,138,0.95)',
        shadow: 'rgba(13,47,138,0.95)',
        title: '地块',
        link: 'place-a',
        level: mid
      }, {
        image: 'icon_crop.png',
        color: 'rgba(13,47,138,0.95)',
        shadow: 'rgba(13,47,138,0.95)',
        title: '作物',
        link: 'crop',
        level: mid
      }, {
        image: 'icon_fungus.png',
        color: 'rgba(13,47,138,0.95)',
        shadow: 'rgba(13,47,138,0.95)',
        title: '菌类',
        link: 'fungus',
        level: mid
      }, {
        image: 'icon_livestock.png',
        color: 'rgba(13,47,138,0.95)',
        shadow: 'rgba(13,47,138,0.95)',
        title: '牲畜',
        link: 'livestock',
        level: mid
      }, {
        image: 'icon_good.png',
        color: 'rgba(13,47,138,0.95)',
        shadow: 'rgba(13,47,138,0.95)',
        title: '商品',
        link: 'good',
        level: mid
      },
      // {
      //   image: 'icon_workshop.png',
      //   color: 'rgba(13,47,138,0.95)',
      //   shadow: 'rgba(13,47,138,0.95)',
      //   title: '车间',
      //   link: 'workshop',
      //   level: mid
      // }, 
      {
        image: 'icon_warehouse.png',
        color: 'rgba(13,47,138,0.95)',
        shadow: 'rgba(13,47,138,0.95)',
        title: '仓库',
        link: 'warehouse',
        level: mid
      }, {
        image: 'icon_certification.png',
        color: 'rgba(13,47,138,0.95)',
        shadow: 'rgba(13,47,138,0.95)',
        title: '证书',
        link: 'certification',
        level: mid
      }, {
        image: 'icon_claim.png',
        color: 'rgba(13,47,138,0.95)',
        shadow: 'rgba(13,47,138,0.95)',
        title: '认领',
        link: 'claim',
        level: normal
      }, {
        image: 'icon_scan.png',
        color: 'rgba(13,47,138,0.95)',
        shadow: 'rgba(13,47,138,0.95)',
        title: '扫码',
        link: 'scan',
        level: normal
      }, {
        image: 'icon_setting.png',
        color: 'rgba(13,47,138,0.95)',
        shadow: 'rgba(13,47,138,0.95)',
        title: '设置',
        link: 'setting',
        level: normal
      }
    ];
    this.setData({
      items
    });
  },
  formSubmit(e) {
    let that = this;
    let type = e.currentTarget.dataset.type;
    switch (type) {
      case 'bindAccount':
        that.bindAccount(e.detail.value);
        break;
      case 'createAccount':
        that.createAccount(e.detail.value);
        break;
    }
  },
  bindAccount(value) {
    let that = this;
    let account = value.account.trim();
    let password = value.psd.trim();
    let openid = this.data.openid;
    if (account === '' || password === '') {
      util.showToast('请输入账号密码！', 'none');
      return;
    }
    wx.showLoading({
      title: '绑定中...',
    });
    api.bindAccount({
      data: {
        openid,
        account,
        password
      },
      success(res) {
        console.log(res.data);
        wx.hideLoading();
        let state = res.data;
        if (state.type === 1) {
          util.showToast('账号或密码错误！', 'none');
          return;
        } else if (state.type === 2) {
          util.showToast('该账号已被绑定！', 'none');
          return;
        } else if (state.flag === 'false') {
          util.showToast('网络错误！', 'none');
          return;
        } else {
          that.setData({
            isBind: true,
            role: state.role
          });
          util.setLocalStorage([
            ['userId', state.account.account],
            ['isBind', true],
            ['role', state.role]
          ]);
          that.init();
        }
      }
    });
  },
  createAccount(value) {
    let that = this;
    let openid = this.data.openid;
    let account = value.account.trim();
    let userName = value.userName.trim();
    let password = value.psd.trim();
    let confirm = value.confirm.trim();
    let email = value.email.trim();
    let phone = value.phone.trim();

    if (account === '' || userName === '' || password === '' || confirm === '') {
      util.showToast('请将信息完善！', 'none');
      return;
    }
    if (password !== confirm) {
      util.showToast('两次密码不一致！', 'none');
      return;
    }
    wx.showLoading({
      title: '创建中...',
    });
    api.createAccount({
      data: {
        openid,
        account,
        userName,
        password,
        email,
        phone
      },
      success(res) {
        console.log(res.data);
        wx.hideLoading();
        let state = res.data;
        if (state.type === 1) {
          util.showToast('网络错误！', 'none');
          return;
        } else if (state.type === 2) {
          util.showToast('账户已经存在！', 'none');
          return;
        } else {
          that.setData({
            isBind: true,
            role: state.role
          });
          util.setLocalStorage([
            ['userId', state.account.account],
            ['isBind', true],
            ['role', state.role]
          ]);
          that.init();
        }
      }
    });
  },
  bindRole() {
    let that = this;
    wx.getStorage({
      key: 'role',
      success(res) {
        that.setData({
          role: res.data
        });
        that.init();
      }
    });
  },
  goToSignInPage() {
    let login = this.data.login;
    if (!login) {
      wx.navigateTo({
        url: '/pages/sign_in/sign_in',
      })
    }
    // else if(){
    //   let account = this.data.user.account;
    //   wx.navigateTo({
    //     url: `/pages/userinfo/userinfo?`,
    //   })
    // }
  },
  goToLinkPage(e) {
    let link = e.currentTarget.dataset.link;
    if (link === 'scan') {
      wx.scanCode({
        success(res) {
          console.log(res);
          let data = res.result;
          let url = data.substring(data.lastIndexOf('?'), data.length);
          let json = util.getQueryStringArgs(url);
          console.log(json);
          wx.navigateTo({
            url: `/pages/suyuan-detail/suyuan-detail?id=${json.id}&type=${json.type}`,
          })
        }
      })
    } else {
      wx.navigateTo({
        url: `/pages/${link}/${link}`
      })
    }
  },
  goToUserQRCodePage() {
    wx.getStorage({
      key:'userId',
      success(res) {
        wx.navigateTo({
          url: `/pages/userqrcode/userqrcode?account=${res.data}`,
        })
      }
    })
  },
  slide(e) {
    this.setData({
      current: e.currentTarget.dataset.index
    });
  },
  change(e) {
    this.setData({
      current: e.detail.current
    });
    this.animation(e.detail.current * 50 + '%');
  },
  animation(index) {
    let animation = wx.createAnimation({
      duration: 400,
      timingFunction: 'linear',
      delay: 0
    });
    animation.left(index).step();
    this.setData({
      animation: animation.export()
    });
  }
})